function deleteParamFromURL(url, name)
{
    var result = url;
    var queryStr = '';
    var pathStr = '';
    var queryBeg = null;
    var fragmentStr = '';
    var fragmentBeg = null;
    var queryAray = new Array();
    if ((queryBeg = url.indexOf('?')) != -1) {
        pathStr = url.substring(0, queryBeg);
        if ((fragmentBeg = url.indexOf('#', queryBeg)) != -1) {
            queryStr = url.substring(queryBeg+1, fragmentBeg);
            fragmentStr = url.substring(fragmentBeg+1, url.length-1);
        } else {
            queryStr = url.substring(queryBeg+1, url.length-1);
        }
        queryAray = queryStr.split('&');
        for (var i = 0; i < queryAray.length; i++) {
            if ((queryAray[i] == name) || (queryAray[i].indexOf(name + '=') == 0)) {
                queryAray.splice(i, 1);
            }
        }
        result = pathStr + (queryAray.length ? '?' + queryAray.join('&') : '') + (fragmentStr.length ? '#' + fragmentStr : '');
    }
    return result;
}

//===  toggleNode  class BEGIN  ================================================

function toggleNode(params)
{
    if (typeof params == 'undefined') {
        throw "";
    } else if (typeof params.node == 'undefined') {
        throw "";
    }
    this.node = params.node;
    this.parent = this.node.parentNode;
    this.next = this.node.nextSibling;
    this.showed = true;

    if (typeof params.OnBeforeHide != 'undefined') {
        this.OnBeforeHide = params.OnBeforeHide;
    }

    if (typeof params.OnAfterHide != 'undefined') {
        this.OnAfterHide = params.OnAfterHide;
    }

    if (typeof params.OnBeforeShow != 'undefined') {
        this.OnBeforeShow = params.OnBeforeShow;
    }

    if (typeof params.OnAfterShow != 'undefined') {
        this.OnAfterShow = params.OnAfterShow;
    }

    if ((typeof params.hide == 'undefined') ? true : params.hide) {
        this.hide();
    }
}
toggleNode.prototype.hide = function()
{
    if (this.showed) {
        if (typeof this.OnBeforeHide != 'undefined') {
            if (! this.OnBeforeHide()) {
                return;
            }
        }
        this.node = this.parent.removeChild(this.node);
        this.showed = false;
        if (typeof this.OnAfterHide != 'undefined') {
            this.OnAfterHide();
        }
    }
}
toggleNode.prototype.show = function()
{
    if (! this.showed) {
        if (typeof this.OnBeforeShow != 'undefined') {
            if (! this.OnBeforeShow()) {
                return;
            }
        }
        /*
        Необходимо следить, что бы согласовать изменения структуры
        и менять соответственно next_secureAnsverInputNode
        */
        if (this.next) {
            this.node = this.parent.insertBefore(
                this.node,
                this.next
            );
        } else {
            this.node = this.parent.appendChild(this.node);
        }
        this.showed = true;
        if (typeof this.OnAfterShow != 'undefined') {
            this.OnAfterShow();
        }
    }
}
toggleNode.prototype.toggle = function()
{
    if (this.showed) {
        this.hide();
    } else {
        this.show();
    }
}

//===  toggleNode  class END  ==================================================

function replaceHtmlById(id, html)
{
    var element = document.getElementById(id);
    var parent = element.parentNode;
    var next = element.nextSibling;
    parent.removeChild(element);
    document.open();
    document.write(html);
    document.open();
    document.close();
    var new_element = document.getElementById(id);
    if (!(next == null)) {
        alert('Next exists: "'+next+'"');
        //new_element = parent.removeChild(new_element);
        //alert([parent, new_element, next]);
        //new_element = parent.insertBefore(new_element, next);
    }
    return new_element;
}

function setAnswerToInput(input, code)
{
    if (typeof code != 'string') {
        return false;
    }
    input.value = code;
    return true;
}

function doc_write(code)
{
    document.open();
    document.write(code);
    document.close();
}

function remove_node(node)
{
    node.parentNode.removeChild(node);
}

function sys_onLoad()
{
   // sys_set_confirm_discard_on_links();
}

function confirm_discard_changes(str)
{
    return confirm(str);
}

function quit_from_form()
{
    var str = false;
    if ((str = check_changes_for_discard()) !== false) {
        return confirm_discard_changes(str);
    } else {
        return true;
    }
}

function check_changes_for_discard()
{
    var result = false;
    if (__sys_edited_forms) {
        if (__sys_edited_forms.length) {
            var last = 0;
            var index = null;
            for (var i = 0; i < __sys_edited_forms.length; i++) {
                if (last < __sys_edited_forms[i].num) {
                    index = i;
                }
            }
            if (index != null) {
                result = __sys_edited_forms[index].str;
            }
        }
    }
    return result;
}

function sys_set_confirm_discard_on_links()
{
    var links = document.getElementsByTagName('a');
    if (links) {
        for (var i = 0; i < links.length; i++) {
            links.item(i).setAttribute('onclick', 'if (!quit_from_form()) return false;' + links.item(i).getAttribute('onclick'));
        }
    }
}

function __sys_editForm_changes(__id, __str)
{
    __sys_trf_editForm_del_changes(__id);
    __sys_edited_forms[__sys_edited_forms.length] = {ident: __id, str: __str, num: __sys_edited_forms.length + 1};
}

function __sys_editForm_del_changes(__id)
{
    for (var i = 0; i < __sys_edited_forms.length; i++) {
        if (__sys_edited_forms[i].ident == __id) {
            delete __sys_edited_forms[i];
        }
    }
}

function __sys_editForm_is_changes(__id)
{
    for (var i = 0; i < __sys_edited_forms.length; i++) {
        if (__sys_edited_forms[i].ident == __id) {
            return true;
        }
    }
    return false;
}

var __sys_edited_forms = [];

var __toggleBlockFlags = [];
function toggleBlock(id, linkHTML, captionEnabled)
{
    var index = undefined;
    for (var i = 0; i < __toggleBlockFlags.length; i++) {
        if ((__toggleBlockFlags[i].identifier != undefined) && (__toggleBlockFlags[i].identifier == id)) {
            index = i;
            break;
        }
    }

    var toggledElement = $("#"+id);

	if (index == undefined) {
	    index = __toggleBlockFlags.length;
	    linkHTML = linkHTML||'';
	    var buttonDOM = $(linkHTML);
	    var linkDOM = buttonDOM.find('a');
	    linkDOM.attr('id', 'link4'+id);
	    linkDOM.click(function () {
            toggleBlock(eval('id'));
            return false;
        });

	    try {
    		buttonDOM.insertAfter(toggledElement);
    		__toggleBlockFlags[index] = {
                identifier: id,
                showen: false,
                captionEnabled: captionEnabled,
                captionDisabled: linkDOM.text()
            };
    		toggledElement.hide();
	    } catch (e) {}
	} else {
	    var linkElement = $("#link4"+id);
		if (__toggleBlockFlags[index].showen) {
			toggledElement.hide();
			__toggleBlockFlags[index].showen = false;
			linkElement.text(__toggleBlockFlags[index].captionDisabled);
		} else {
			toggledElement.fadeIn("slow");
			__toggleBlockFlags[index].showen = true;
			linkElement.text(__toggleBlockFlags[index].captionEnabled);
		}
	}
}
function __sys_checkMessages(__url)
{
	setTimeout(checkMessages, 500000);
	$.ajax({
	    type: "POST",
   		url: __url,
	   	dataType: "html",
   		success:
   			function(data)
	   		{
				$("#menu-item-messages").html(data);
			},
   		beforeSend: function(data){}
	});
}

function drawSum(value)
{
    value = isNaN(value) || value == null || value == undefined ? 0 : value;
    var result = value.toFixed(2);
    var int_part = result.split('.');
    var dec_part = int_part[1];
        int_part = int_part[0];

    var parts = [];
    for (var i = 0; i < int_part.length; i += 3) {
        parts[parts.length] = int_part.substring(int_part.length-i-3, int_part.length-i);
    }

    var result = '';
    for (i = 0; i < parts.length; i++) {
        result = '<span class="group">' + parts[i] + '</span>' + result;
    }
    result = '<span class="int-part">' + result + '</span>';

    return $('<span>' + result + ',' + dec_part + '</span>');
}

function __sys_feeCaptionClass(container)
{
    this.container = $(container);
    this.draw = function(value)
    {
        this.container.html(drawSum(value));
    }
}

function formValueOf(form, name)
{
    if (typeof name != 'string') {
        throw "formValueOf(): Bad argument “name”. Must be string.";
    }
    if (form.elements) {
        var element = form.elements[name];
        if (element == undefined) {
            return undefined;
        }
        if (element.length) {
            var searche_radio = false;
            for (var i = 0; i < element.length; i++) {
                var button = element[i];

                if (button.type == 'radio') {
                    searche_radio = true;
                    if (button.checked) {
                        return button.value;
                    }
                }
            }
            if (! searche_radio) {
                return undefined;
            } else {
                return null;
            }
        } else if (element.type == 'radio') {
            if (element.checked) {
                return element.value;
            } else {
                return null;
            }
        } else {
            return element.value;
        }
    } else {
        throw "formValueOf(): Bad argument “form”. Must be instance of HTMLFormElement.";
    }

    return null;
}

function DOMElement(name)
{
    return document.createElement(name);
}

function DOMAttr(name, value)
{
    var result = document.createAttribute(name);
    result.value = value;
    return result;
}

function DOMText(string)
{
    return document.createTextNode(string);
}

function DOMAdd(node, child)
{
    if (child.nodeType == 2) {
        node.setAttribute(child.nodeName, child.value);
        return node;
    } else {
        return node.appendChild(child);
    }
}

function parse_decimal_str(number, dec_sep)
{
    var result = {
        negative: false,
        groups: [],
        int_part: "",
        dec_part: ""
    };

    number = new String(number);
    var ar = /([-+−–—]?)(\d*)((.\d*)?)/m.exec(number);
    if (ar == null) {
        throw "parse_decimal_str(): Bad argument “number”. Must be numerical.";
    }
    result.negative = (ar[1].length && (ar[1] != '+'));
    result.int_part = ar[2].length ? ar[2] : '0';
    result.dec_part = (ar[3].length > 1) ? ar[3].substr(1) : '0';

    var seperator = ' ';
    var int_part = result.int_part;
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(int_part)) {
        int_part = int_part.replace(rgx, '$1' + seperator + '$2');
    }
    result.groups = int_part.split(seperator);

    return result;
}

function format_decimal(parsed)
{
    var result = "";
    result += parsed.negative ? '−' : '';
    result += parsed.groups.join(" ");
    result += ',';
    result += parsed.dec_part.length == 1 ? parsed.dec_part + '0' : parsed.dec_part;

    return result;
}

function plurar_mode_ru(n)
{
    return (n == 1)
        ? 0
        : ((n % 10 == 1) && (n % 100 != 11))
            ? 1
            : ((n % 10 >= 2) && (n % 10 <= 4) && ((n % 100 < 10) || ($n % 100 > 20)))
                ? 2
                : 3;
}

function getBrowserName()
{
    var ua = navigator.userAgent.toLowerCase();
    // Определим Internet Explorer
    if (ua.indexOf("msie") != -1 && ua.indexOf("opera") == -1 && ua.indexOf("webtv") == -1) {
        return "msie"
    }
    // Opera
    if (ua.indexOf("opera") != -1) {
        return "opera"
    }
    // Gecko = Mozilla + Firefox + Netscape
    if (ua.indexOf("gecko") != -1) {
        return "gecko";
    }
    // Safari, используется в MAC OS
    if (ua.indexOf("safari") != -1) {
        return "safari";
    }
    // Konqueror, используется в UNIX-системах
    if (ua.indexOf("konqueror") != -1) {
        return "konqueror";
    }
    return "unknown";
}